package com.capgemini.drms.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.capgemini.drms.dao.DrmsDemandDao;
import com.capgemini.drms.dao.DrmsDesigationDao;
import com.capgemini.drms.dao.DrmsResourceDao;
import com.capgemini.drms.dao.WeeklyReportDao;
import com.capgemini.drms.model.DrmsDemand;
import com.capgemini.drms.model.WeeklyReport;
import com.capgemini.drms.service.WeeklyReportService;

@Service(value = "WeeklyReportService")
public class WeeklyReportServiceImpl implements WeeklyReportService {
	@Autowired
	WeeklyReportDao weeklyReportDao;
	@Autowired
	DrmsDesigationDao drmsDesigationDao;
	@Autowired
	DrmsResourceDao drmsResourceDao;
	@Autowired
	DrmsDemandDao drmsDemandDao;

	@Override
	public List<WeeklyReport> getWeeklyReport() {

		return weeklyReportDao.getWeeklyReport();
	}

	
	@Override
	public Map<String, Map<Double, String>> getSummaryDesignationReportplus() {
		Map<String, Map<Double, String>> map = new LinkedHashMap<>();
		double sum = 0.0;
		List<WeeklyReport> weeklyReports = getWeeklyReport();
		List<String> listDesignation = drmsDesigationDao.getAllDesigation();
		listDesignation.remove(0);
		listDesignation.remove(0);
		listDesignation.remove(0);
		for (String designation : listDesignation) {
			
			Map<Double, String> innerMap = new HashMap<>();
			StringBuilder sb = new StringBuilder();
			boolean judge = false;
			int temp = 1;
			double account = 0.0;
			for (WeeklyReport weeklyReport : weeklyReports) {
				boolean flag = false;
				if (weeklyReport.getDesignation().equals(designation)) {
					account++;
					sum++;
					String employeeId = weeklyReport.getKinId();
					int resourceId = drmsResourceDao
							.findDrmsResourceIdByEmployeeId(employeeId);
					List<DrmsDemand> demands = drmsDemandDao
							.findDemandByResourceId(resourceId);
					for (DrmsDemand drmsDemand : demands) {
						String location = drmsDemand.getDrmsSite()
								.getSiteName().trim();
						if (!(location.equals("Hangzhou"))) {
							flag = true;
						}
					}
					if (flag) {
						if (temp != 1)
							sb.append(" , ");
						sb.append(weeklyReport.getName());
						temp++;
						judge = true;
					}
				}

			}
			if (judge) {
				innerMap.put(account, sb.toString());
			} else {
				innerMap.put(account, " ");
			}
			/* map.put(designation.replaceAll(" ", ""), account); */
			map.put(designation, innerMap);

		}
		Map<Double, String> tempMap = new HashMap<>();
		tempMap.put(sum, " NOT IN Hangzhou ");
		map.put("Total", tempMap);
		
		return map;
	}

	@Override
	public Map<String, Double> getSummaryCapabilityReport() {
		Map<String, Double> map = new LinkedHashMap<>();
		double sum = 0.0;
		List<WeeklyReport> weeklyReports = getWeeklyReport();
		List<String> listCapability = new ArrayList<>();
		for (WeeklyReport weeklyReport : weeklyReports) {
			listCapability.add(weeklyReport.getCapability());
		}
		HashSet<String> h2 = new HashSet<String>(listCapability);
		listCapability.clear();
		listCapability.addAll(h2);

		for (String capability : listCapability) {

			double account = 0.0;
			for (WeeklyReport weeklyReport : weeklyReports) {
				if (weeklyReport.getCapability().equals(capability)) {
					account++;
					sum++;
				}

			}
			map.put(capability.replaceAll(" ", ""), account);

		}
		map.put("Total", sum);
		return map;
	}


	@Override
	public Collection<Double> getSummaryDesignationReport() {
		Map<String, Double> map = new LinkedHashMap<>();
		
		List<WeeklyReport> weeklyReports = getWeeklyReport();
		List<String> listDesignation = drmsDesigationDao.getAllDesigation();
		for (String designation : listDesignation) {
			double account = 0.0;
			for (WeeklyReport weeklyReport : weeklyReports) {
				if (weeklyReport.getDesignation().equals(designation)) {
					account++;	
				}
			}
			map.put(designation, account);
		}
		return map.values();
	}


	@Override
	public List<Map<Object, Object>> getSummaryCapabilityReportPie() {
		List<Map<Object, Object>> resultList =new ArrayList<>();
		Map<Object, Object> map = new LinkedHashMap<>();
		double sum = 0.0;
		List<WeeklyReport> weeklyReports = getWeeklyReport();
		List<String> listCapability = new ArrayList<>();
		for (WeeklyReport weeklyReport : weeklyReports) {
			listCapability.add(weeklyReport.getCapability());
		}
		HashSet<String> h2 = new HashSet<String>(listCapability);
		listCapability.clear();
		listCapability.addAll(h2);

		for (String capability : listCapability) {

			double account = 0.0;
			for (WeeklyReport weeklyReport : weeklyReports) {
				if (weeklyReport.getCapability().equals(capability)) {
					account++;
					sum++;
				}

			}
			map.put(capability.replaceAll(" ", ""), account);

		}
		
		for (Map.Entry<Object,Object> entry : map.entrySet()) {
			Map<Object,Object> tempMap= new HashMap<Object,Object>();
			tempMap.put("name", entry.getKey());
			tempMap.put("y", (Double.parseDouble((entry.getValue()).toString())));
			resultList.add(tempMap);
		}
		
		
		return resultList;
	}


	@Override
	public List<Map<Object, Object>> getSummaryLocationReportPie() {
		List<Map<Object, Object>> resultList =new ArrayList<>();
		Map<Object, Object> map = new LinkedHashMap<>();
		double sum = 0.0;
		List<WeeklyReport> weeklyReports = getWeeklyReport();
		List<String> listLocation = new ArrayList<>();
		for (WeeklyReport weeklyReport : weeklyReports) {
			listLocation.add(weeklyReport.getLocation());
		}
		HashSet<String> h2 = new HashSet<String>(listLocation);
		listLocation.clear();
		listLocation.addAll(h2);

		for (String location : listLocation) {

			double account = 0.0;
			for (WeeklyReport weeklyReport : weeklyReports) {
				if (weeklyReport.getLocation().equals(location)) {
					account++;
					sum++;
				}

			}
			map.put(location.replaceAll(" ", ""), account);

		}
		
		for (Map.Entry<Object,Object> entry : map.entrySet()) {
			Map<Object,Object> tempMap= new HashMap<Object,Object>();
			tempMap.put("name", entry.getKey());
			tempMap.put("y", (Double.parseDouble((entry.getValue()).toString())));
			resultList.add(tempMap);
		}
		
		
		return resultList;
	}

	

}
